﻿
using Newtonsoft.Json;
using QCloud.CosApi.Api;
using QCloud.CosApi.Common;
using System;
using System.Collections.Generic;


namespace cos_dotnet_sdk
{
    public class COSTool
    {
        const int APP_ID = 1256351978;
        const string SECRET_ID = "AKIDjHadT49WNzMTN4wllX5PUM1RcaKq9C0a";
        const string SECRET_KEY = "ThhT1LRKzQcaJp79vTlpeDXQ4SbU7Veh";

        const string hongfundpiaojuBucketName = "hongfundpiaoju";

        static UploadLogger ul = new UploadLogger("ShangChuanLog");
        //创建cos对象
        static CosCloud cosPublic;


        static COSTool()
        {
            //	广州(华南) （gz）
            cosPublic = new CosCloud("gz", APP_ID, SECRET_ID, SECRET_KEY);
            //北京 （bj）
        }

        public static bool UploadFileForPublic(string remotePath, string localPath)
        {
            return UploadFile(hongfundpiaojuBucketName, remotePath, localPath);
        }



        private static bool UploadFile(string bucketName, string remotePath, string localPath)
        {
            //上传文件（不论文件是否分片，均使用本接口）
            var uploadParasDic = new Dictionary<string, string>();
            uploadParasDic.Add(CosParameters.PARA_BIZ_ATTR, "");
            uploadParasDic.Add(CosParameters.PARA_INSERT_ONLY, "0");
            //uploadParasDic.Add(CosParameters.PARA_SLICE_SIZE,SLICE_SIZE.SLIZE_SIZE_3M.ToString());

            string result;
            try
            {
                var cos = cosPublic;

                ul.Write("bucketName:" + bucketName + " remotePath:" + remotePath + " localPath:" + localPath + " uploadParasDic:" + uploadParasDic);
                Console.WriteLine("bucketName:" + bucketName + " remotePath:" + remotePath + " localPath:" + localPath + " uploadParasDic:" + uploadParasDic);
                result = cos.UploadFile(bucketName, remotePath, localPath, uploadParasDic);
            }
            catch (Exception ex)
            {
                ul.Write("COSTool.UploadFile" + ex);
                Console.WriteLine("COSTool.UploadFile" + ex);
                return false;
            }

            if (string.IsNullOrEmpty(result))
            {
                ul.Write("上传文件返回结果为空");
                Console.WriteLine("上传文件返回结果为空");
                return false;
            }

            //LogTool.WriteLog("上传完成\r\n" + result, "cos_result.txt");

            COSResult cosResult;
            try
            {
                cosResult = JsonConvert.DeserializeObject<COSResult>(result);
            }
            catch (Exception ex)
            {
                ul.Write("COSTool.UploadFile" + ex);
                Console.WriteLine("COSTool.UploadFile" + ex);
                return false;
            }

            if (cosResult == null)
            { 
                ul.Write("上传文件返回结果为转换成对象为null");
                Console.WriteLine("上传文件返回结果为转换成对象为null");
                return false;
            }

            if (cosResult.code == 0)
            {
                return true;
            }

            ul.Write("上传文件返回结果为失败(code:" + cosResult.code + ",message:" + cosResult.message + ",data:" + cosResult.data + ")");
            Console.WriteLine("上传文件返回结果为失败(code:" + cosResult.code + ",message:" + cosResult.message + ",data:" + cosResult.data + ")");
            return false;
        }


    }

    //{"code":0,"message":"SUCCESS","request_id":"","data":{"access_url":"","resource_path":"","source_url":"","url":"","vid":""}}
    public class COSResult
    {
        //code Int 是 错误码，成功时为0
        public int? code;
        public string message;
        //private string request_id;
        //data    Array 是   返回数据，请参考《Restful API 创建文件》
        public COSResultData data;
    }

    public class COSResultData
    {
        public string access_url;
        public string resource_path;
        public string source_url;
        public string url;
        public string vid;
    }
}
